<div class="m-bg">
<h1>MySQL DATE_SUB() 函数</h1>
<h2>定义和用法</h2> <p>DATE_SUB() 函数从日期减去指定的时间间隔。</p> <h3><b>语法</b></h3> <div class="code notranslate"><pre><code class="language-sql"><div> DATE_SUB(date,INTERVAL expr type)</div></code></pre></div> <p>date 参数是合法的日期表达式。expr 参数是您希望添加的时间间隔。</p> <p>type 参数可以是下列值：</p> <table class="reference notranslate">
<tr><th align="left">Type 值</th></tr>
<tr><td>MICROSECOND</td></tr>
<tr><td>SECOND</td></tr>
<tr><td>MINUTE</td></tr>
<tr><td>HOUR</td></tr>
<tr><td>DAY</td></tr>
<tr><td>WEEK</td></tr>
<tr><td>MONTH</td></tr>
<tr><td>QUARTER</td></tr>
<tr><td>YEAR</td></tr>
<tr><td>SECOND_MICROSECOND</td></tr>
<tr><td>MINUTE_MICROSECOND</td></tr>
<tr><td>MINUTE_SECOND</td></tr>
<tr><td>HOUR_MICROSECOND</td></tr>
<tr><td>HOUR_SECOND</td></tr>
<tr><td>HOUR_MINUTE</td></tr>
<tr><td>DAY_MICROSECOND</td></tr>
<tr><td>DAY_SECOND</td></tr>
<tr><td>DAY_MINUTE</td></tr>
<tr><td>DAY_HOUR</td></tr>
<tr><td>YEAR_MONTH</td></tr>
</table>
<h3><b>实例</b></h3> <p>假设我们有如下的 "Orders" 表：</p> <table class="reference notranslate">
<tr>
<th align="left">OrderId</th> <th align="left">ProductName</th> <th align="left">OrderDate</th> </tr>
<tr>
<td>1</td> <td>Jarlsberg Cheese</td> <td>2008-11-11 13:23:44.657</td> </tr>
</table>
<p>现在，我们想要向 "OrderDate" 减去 5 天。</p> <p>我们使用下面的 SELECT 语句：</p> <div class="code notranslate"><pre><code class="language-sql"><div> SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 5 DAY) AS SubtractDate<br/> FROM Orders</div></code></pre></div> <p>结果：</p> <table class="reference notranslate">
<tr>
<th align="left">OrderId</th> <th align="left">SubtractDate</th> </tr>
<tr>
<td>1</td> <td>2008-11-06 13:23:44.657</td> </tr>
</table>
<div class="text-center padding-10 margin-t-5">
</div>
</div>